/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TambahGajiGUI.java
 *
 * Created on 25 Jul 12, 15:47:41
 */
package gui;

import dao.GajiDAO;
import dao.GolonganDAO;
import dao.KaryawanDAO;
import entity.Gaji;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;

/**
 *
 * @author Bramso
 */
public class UbahGajiGUI extends javax.swing.JInternalFrame {

    EntityManager em;
    boolean valid = false;

    /** Creates new form TambahGajiGUI */
    public UbahGajiGUI(EntityManager em) {
        initComponents();

        this.em = em;
        this.bindingKode();
    }

    public void bindingKode() {
        GajiDAO dao = new GajiDAO(em);
        kodegaji_cmb.removeAllItems();
        for (int i = 0; i < dao.getAll().size(); i++) {
            kodegaji_cmb.addItem(dao.getAll().get(i).getKode());

        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        nama_txt = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        gajipokok_txt = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        pajak_txt = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        tunjangan1_txt = new javax.swing.JTextField();
        tunjangan2_txt = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        hari_txt = new javax.swing.JFormattedTextField();
        lembur_txt = new javax.swing.JTextField();
        cek_btn = new javax.swing.JButton();
        reset_btn = new javax.swing.JButton();
        keluar_btn = new javax.swing.JButton();
        golongan_txt = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        kodegaji_cmb = new javax.swing.JComboBox();
        bulan_txt = new javax.swing.JTextField();

        setTitle("Ubah Gaji");

        jLabel2.setText("Nama");

        nama_txt.setEditable(false);

        jLabel3.setText("Bulan");

        jLabel4.setText("Golongan");

        jLabel5.setText("Gaji Pokok");

        gajipokok_txt.setEditable(false);

        jLabel6.setText("Pajak (%)");

        pajak_txt.setEditable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tunjangan"));

        tunjangan1_txt.setText("0");

        tunjangan2_txt.setText("0");

        jLabel9.setText("Tunjangan 1");

        jLabel10.setText("Tunjangan 2");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(tunjangan1_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(18, 18, 18)
                        .addComponent(tunjangan2_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(tunjangan1_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(tunjangan2_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(50, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Bonus"));

        jLabel7.setText("Upah Lembur");

        jLabel8.setText("Jumlah Hari");

        hari_txt.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        hari_txt.setText("0");
        hari_txt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                hari_txtActionPerformed(evt);
            }
        });

        lembur_txt.setText("0");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(hari_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lembur_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(lembur_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(hari_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        cek_btn.setText("Cek");
        cek_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cek_btnActionPerformed(evt);
            }
        });

        reset_btn.setText("Reset");
        reset_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reset_btnActionPerformed(evt);
            }
        });

        keluar_btn.setText("Keluar");
        keluar_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluar_btnActionPerformed(evt);
            }
        });

        golongan_txt.setEditable(false);

        jLabel11.setText("Kode Gaji");

        kodegaji_cmb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kodegaji_cmbActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(27, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cek_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(reset_btn)
                                .addGap(18, 18, 18)
                                .addComponent(keluar_btn))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel11))
                        .addGap(32, 32, 32)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(kodegaji_cmb, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 130, Short.MAX_VALUE)
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(bulan_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(82, 82, 82))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(nama_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(golongan_txt, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(gajipokok_txt, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(pajak_txt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(239, Short.MAX_VALUE))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(kodegaji_cmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(bulan_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nama_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(golongan_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(gajipokok_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(pajak_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cek_btn)
                    .addComponent(reset_btn)
                    .addComponent(keluar_btn))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void hari_txtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_hari_txtActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_hari_txtActionPerformed

    private void keluar_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluar_btnActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_keluar_btnActionPerformed

    private void reset_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reset_btnActionPerformed
        // TODO add your handling code here:
        
        tunjangan1_txt.setText("0");
        tunjangan2_txt.setText("0");
        lembur_txt.setText("0");
        hari_txt.setText("0");

        valid = false;

    }//GEN-LAST:event_reset_btnActionPerformed

    private void cek_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cek_btnActionPerformed
        // TODO add your handling code here:
        try {
            

            //Tampung data
            String kodeGaji = kodegaji_cmb.getSelectedItem().toString();
            
            String m1 = "Kode Gaji      :   "+kodeGaji+"\n";
            String m2 = "Bulan          :   "+bulan_txt.getText()+"\n";
            String m3 = "Nama           :   "+nama_txt.getText()+"\n";
            String m4 = "Golongan       :   "+golongan_txt.getText()+"\n";
            String m5 = "Gaji Pokok     :   "+gajipokok_txt.getText()+"\n";
            String m6 = "Pajak (%)      :   "+pajak_txt.getText()+"\n";
            String m7 = "Tunjangan 1    :   "+tunjangan1_txt.getText()+"\n";
            String m8 = "Tunjangan 2    :   "+tunjangan2_txt.getText()+"\n";
            String m9 = "Upah Lembur    :   "+lembur_txt.getText()+"\n";
            String m10= "Jumlah Hari  :   "+hari_txt.getText()+"\n";
            
            double hitung = (Double.parseDouble(gajipokok_txt.getText())+Double.parseDouble(tunjangan1_txt.getText())+Double.parseDouble(tunjangan2_txt.getText())
                    +(Double.parseDouble(lembur_txt.getText())*Double.parseDouble(hari_txt.getText())))
                    -((Double.parseDouble(pajak_txt.getText())/100)*Double.parseDouble(gajipokok_txt.getText()));
            
            String m11 ="Gaji Bersih    :   "+String.valueOf(hitung);
            
            String message = m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+m11;
            int result = JOptionPane.showConfirmDialog(this, message, "Konfirmasi", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {
                try {
                    GajiDAO dao = new GajiDAO(em);
                    Gaji g = new Gaji();
                    
                    g.setKode(kodeGaji);
                    g.setBulan(bulan_txt.getText());
                    g.setNama(nama_txt.getText());
                    g.setGolongan(golongan_txt.getText());
                    g.setGajipokok(Double.parseDouble(gajipokok_txt.getText()));
                    g.setPajak(Double.parseDouble(pajak_txt.getText()));
                    g.setTunjangan1(Double.parseDouble(tunjangan1_txt.getText()));
                    g.setTunjangan2(Double.parseDouble(tunjangan2_txt.getText()));
                    g.setBonus(Double.parseDouble(lembur_txt.getText())*Double.parseDouble(hari_txt.getText()));
                    g.setGajibersih(hitung);
                    
                    JOptionPane.showMessageDialog(this, dao.Update(kodegaji_cmb.getSelectedItem().toString(),g));
                    
                    
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, e.getMessage());
                }
            } else {
            }

        } catch (NumberFormatException n) {
            //n.printStackTrace();
            JOptionPane.showMessageDialog(this, "Tunjangan dan Bonus harus Di isi dengan Angka");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
    }//GEN-LAST:event_cek_btnActionPerformed

    private void kodegaji_cmbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kodegaji_cmbActionPerformed
        // TODO add your handling code here:
        GajiDAO g = new GajiDAO(em);
        
        
        bulan_txt.setText(g.getByKode(kodegaji_cmb.getSelectedItem().toString()).get(0).getBulan());
        nama_txt.setText(g.getByKode(kodegaji_cmb.getSelectedItem().toString()).get(0).getNama());
        golongan_txt.setText(g.getByKode(kodegaji_cmb.getSelectedItem().toString()).get(0).getGolongan());
        gajipokok_txt.setText(String.valueOf(g.getByKode(kodegaji_cmb.getSelectedItem().toString()).get(0).getGajipokok()));
        pajak_txt.setText(String.valueOf(g.getByKode(kodegaji_cmb.getSelectedItem().toString()).get(0).getPajak()));
        tunjangan1_txt.setText(String.valueOf(g.getByKode(kodegaji_cmb.getSelectedItem().toString()).get(0).getTunjangan1()));
        tunjangan2_txt.setText(String.valueOf(g.getByKode(kodegaji_cmb.getSelectedItem().toString()).get(0).getTunjangan2()));
        
        
        
    }//GEN-LAST:event_kodegaji_cmbActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField bulan_txt;
    private javax.swing.JButton cek_btn;
    private javax.swing.JTextField gajipokok_txt;
    private javax.swing.JTextField golongan_txt;
    private javax.swing.JFormattedTextField hari_txt;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton keluar_btn;
    private javax.swing.JComboBox kodegaji_cmb;
    private javax.swing.JTextField lembur_txt;
    private javax.swing.JTextField nama_txt;
    private javax.swing.JTextField pajak_txt;
    private javax.swing.JButton reset_btn;
    private javax.swing.JTextField tunjangan1_txt;
    private javax.swing.JTextField tunjangan2_txt;
    // End of variables declaration//GEN-END:variables
}
